home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Sources / UContainer.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  6.0 KB  |  235 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UContainer.cp 
  3. // Copyright © 1995-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6.  
  7.  
  8. #ifndef __UCONTAINER__
  9. #include "UContainer.h"
  10. #endif
  11.  
  12. #if qContainer
  13.  
  14. // MacApp
  15.  
  16. // For CALib we have to break the encapsulation of TWindow and TDocument
  17. // so we can find its CADocumentRef. 
  18.  
  19. #ifndef __UDOCUMENT__
  20. #include "UDocument.h"
  21. #endif
  22.  
  23. #ifndef __UMACAPPGLOBALS__
  24. #include "UMacAppGlobals.h"
  25. #endif
  26.  
  27. #ifndef __UMENUMGR__
  28. #include "UMenuMgr.h"
  29. #endif
  30.  
  31. #ifndef __UWINDOW__
  32. #include "UWindow.h"
  33. #endif
  34.  
  35. // CALib
  36.  
  37. #ifndef _CALIB_
  38. #include "CALib.h"
  39. #endif
  40.  
  41. // Toolbox
  42.  
  43. #ifndef __CODEFRAGMENTS__
  44. #include <CodeFragments.h>
  45. #endif
  46.  
  47. #ifndef __MENUS__
  48. #include <Menus.h>
  49. #endif
  50.  
  51. //========================================================================================
  52. // Global Variables
  53. //========================================================================================
  54.  
  55. Boolean gContainerLib;
  56.  
  57. //========================================================================================
  58. // Global Functions
  59. //========================================================================================
  60.  
  61. static void MARegisterMenu(CACommandID id, CommandNumber number);
  62. static void MARegisterCommand(CACommandID id, CommandNumber number);
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // HasContainerLib: 
  66. //----------------------------------------------------------------------------------------
  67. #pragma segment Main
  68.  
  69. Boolean HasContainerLib()
  70. {
  71. #if qContainer
  72.     static Boolean defined = FALSE;
  73.     static Boolean hasContainerLib;
  74.     if (!defined)
  75.     {
  76.         defined = TRUE;
  77.         hasContainerLib = ((Ptr)CAInit != (Ptr)kUnresolvedCFragSymbolAddress);
  78.     }
  79.     return hasContainerLib;
  80. #else
  81.     return FALSE;
  82. #endif
  83. }
  84.  
  85. //----------------------------------------------------------------------------------------
  86. // InstallContainerLib
  87. //----------------------------------------------------------------------------------------
  88. #pragma segment MAInit
  89.  
  90. void InstallContainerLib()
  91. {
  92.     if (HasContainerLib())
  93.     {
  94.         OSErr theErr;
  95.  
  96.         CAInit();                    // Initialize CALib
  97.         if ((theErr = CAError()) != noErr) return;
  98.  
  99. #if 0 // $$$$$ temporary until implementation is back in CALib
  100.         CAInstallFloatingWindowHandlers(MA_CAFrontWindowProc, MASelectToolboxWindow,
  101.                                         IsFloatWindow);
  102.         if ((theErr = CAError()) != noErr) return;
  103. #endif
  104.         
  105.         gContainerLib = TRUE;
  106.     }
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // MA_CAFrontWindowProc
  111. //----------------------------------------------------------------------------------------
  112. #pragma segment MAWindowRes
  113.  
  114. WindowPtr MA_CAFrontWindowProc(CAWindowLayer windowLayer)
  115. {
  116.     switch (windowLayer)
  117.     {
  118.         case kCADocumentWindow:
  119.             return TWindow::MAFrontWindow();
  120.             
  121.         case kCAFloatingWindow:
  122.             return TWindow::GetFirstFloatingWindowPtr();
  123.  
  124.         default:
  125.             break;
  126.     }
  127.     return NULL;
  128. }
  129.  
  130. //----------------------------------------------------------------------------------------
  131. // GetCADocument
  132. //----------------------------------------------------------------------------------------
  133. #pragma segment MAWindowRes
  134.  
  135. CADocumentRef GetCADocument(WindowPtr wMgrWindow)
  136. {
  137.     CADocumentRef containerDocument = NULL;
  138.     if (wMgrWindow)
  139.     {
  140.         TWindow* window = TWindow::WMgrToWindow(wMgrWindow);
  141.         if (window)
  142.             containerDocument = GetCADocument(window);
  143.     }
  144.     return containerDocument;
  145. }
  146.  
  147. CADocumentRef GetCADocument(TWindow* window)
  148. {
  149.     CADocumentRef containerDocument = NULL;
  150.     if (window)
  151.     {
  152.         TDocument* windowDocument = window->fDocument;
  153.         if (windowDocument)
  154.             containerDocument = windowDocument->GetContainer();
  155.     }
  156.     return containerDocument;
  157. }
  158.  
  159. //========================================================================================
  160. // CLASS CModalFocus
  161. //========================================================================================
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // CModalFocus::CModalFocus: 
  165. //----------------------------------------------------------------------------------------
  166. #pragma segment MAWindowRes
  167.  
  168. CModalFocus::CModalFocus(WindowPtr window)
  169.     : fWindow(window), fHasFocus(FALSE)
  170. {
  171.     // Setup the FailInfo to catch any failures while this object is in scope
  172.     fFailInfo.SetCleanupProc(CModalFocus::CallRelinquish, this);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // CModalFocus::~CModalFocus: 
  177. //----------------------------------------------------------------------------------------
  178. #pragma segment MAWindowRes
  179.  
  180. CModalFocus::~CModalFocus()
  181. {
  182.     FailOSErr(this->Relinquish());
  183.     fFailInfo.Success();
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // CModalFocus::Request: 
  188. //----------------------------------------------------------------------------------------
  189. #pragma segment MAWindowRes
  190.  
  191. Boolean CModalFocus::Request()
  192. {
  193.     Boolean result = FALSE;
  194.     if (fWindow)
  195.     {
  196.         if (gContainerLib)
  197.             result = fHasFocus = CARequestModalFocus(fWindow);
  198.         else
  199.             result = TRUE;
  200.     }
  201.     return result;
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // CModalFocus::Relinquish: 
  206. //----------------------------------------------------------------------------------------
  207. #pragma segment MAWindowRes
  208.  
  209. OSErr CModalFocus::Relinquish()
  210. {
  211.     OSErr err = noErr;
  212.     if (gContainerLib && fWindow && fHasFocus)
  213.     {
  214.         CARelinquishModalFocus(fWindow);
  215.         fHasFocus = FALSE;
  216.         err = CAError();
  217.     }
  218.     return err;
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // CModalFocus::CallRelinquish: 
  223. //----------------------------------------------------------------------------------------
  224. #pragma segment MAWindowRes
  225.  
  226. void CModalFocus::CallRelinquish(void* context)
  227. {
  228.     ((CModalFocus*) context)->Relinquish();
  229. }
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // End of UContainer.cp 
  233.  
  234. #endif // qContainer
  235.